﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cost.Models;
using NBlock.MVCControl;
using System.Data.SqlClient;

namespace Cost.Controllers
{
    public class DishSignDetailController : Controller
    {
        private readonly ICostRepository<DishDeliveryItem> dishdeliveryitemRepository;

        public DishSignDetailController(ICostRepository<DishDeliveryItem> dishdeliveryitemRepository)
        {
            this.dishdeliveryitemRepository = dishdeliveryitemRepository;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult List(nbPageInfo pager, nbSearchCondition where)
        {
            // 如果采购数量为空，则可能是安全库存没有设置。
            string sql = @"select 
MaterialId,Name,sum(DeliveryCount) DeliveryCount
from dbo.DishDeliveryItem i
inner join dbo.DishDelivery d on d.Id = i.orderid
group by MaterialId,Name
order by name";

            List<SqlParameter> plist = new List<SqlParameter>();
            //plist.Add(new SqlParameter("@Date", date.Value));

            var list = ((CostRepository<DishDeliveryItem>)dishdeliveryitemRepository).Context.Database.
                SqlQuery<SimpleDishDeliveryItem>(sql, plist.ToArray()).ToList();

            var fdata = pager.ToGridData(list, list.Count);
            return Json(fdata);
        }
    }

    public class SimpleDishDeliveryItem
    {
        public string MaterialId { get; set; }
        public string Name { get; set; }
        public decimal DeliveryCount { get; set; }
    }
}
